public class MyLinkedList {
    public ListNode head;
    public ListNode last;

    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public void addFirst(int data) {
        //头插法
        ListNode newnode = new ListNode(data);
        if (head == null) {
            head = last = newnode;
        } else {
            newnode.next = head;
            head.prev = newnode;
            head = newnode;
        }
    }

    public void addLast(int data) {
        //尾插法
        ListNode newnode = new ListNode(data);
        if (last == null) {
            head = last = newnode;
        } else {
            last.next = newnode;
            newnode.prev = last;
            last = newnode;
        }
    }

    public void addIndex(int index, int data) {
        //任意位置插入,第一个数据节点为0号下标
        if (index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("index不合法");
        }
        ListNode newnode = new ListNode(data);
        if (head == null) {
            head = last = newnode;
        } else if (index == 0) {
            addFirst(data);
        } else if (index == size()) {
            addLast(data);
        } else {
            ListNode cur = head;
            int count = 0;
            while (count != index) {
                cur = cur.next;
                count++;
            }
            newnode.next = cur;
            cur.prev.next = newnode;
            newnode.prev = cur.prev;
            cur.prev = newnode;
        }
    }

    public boolean contains(int key) {
        //查找是否包含关键字key是否在单链表当中
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int key) {
        //删除第一次出现关键字为key的节点
        if (head == null) {
            throw new NullPointerException("链表已空");
        }
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                if (cur.prev == null && cur.next == null) {
                    head = last = null;
                } else if (cur.prev == null) {
                    head = head.next;
                    head.prev = null;
                } else if (cur.next == null) {
                    cur.prev.next = cur.next;
                    last = last.prev;
                } else {
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }

    public void removeAllKey(int key) {
        //删除所有值为key的节点
        if (head == null) {
            throw new NullPointerException("链表已空");
        }
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                if (cur.prev == null && cur.next == null) {
                    head = last = null;
                    return;
                } else if (cur.prev == null) {
                    head = head.next;
                    head.prev = null;
                    cur = cur.next;
                } else if (cur.next == null) {
                    cur.prev.next = cur.next;
                    last = last.prev;
                    cur = cur.next;
                } else {
                    ListNode next = cur.next;
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                    cur = next;
                }
            }else {
                cur = cur.next;
            }
        }
    }

    public int size() {
        //得到单链表的长度
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        head = last = null;
        //线性表val的数据类型若为引用数据类型，则必须遍历置空，否则内存泄露
    }

}
